package com.shushan.setting;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.Constants;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.setting.bean.RewardBean;
import com.shushan.setting.bean.ScoreTimeBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.DensityUtil;
import com.shushan.util.UmengEventUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName: RewardSettingActivity
 * @Desciption: 学习奖励设置
 * @author: yichaohua
 * @date: 2018-08-14
 */
public class RewardSettingActivity extends NavigationLoseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private static final String TAG = "RewardSettingActivity";

    private Button clearButton;
    private Button submitButton;
    private EditText synScoreEdit;
    private EditText synTimeEdit;
    private EditText whiteScoreEdit;
    private EditText whiteTimeEdit;
    private EditText spokenScoreEdit;
    private EditText spokenTimeEdit;
    private EditText wordScoreEdit;
    private EditText wordTimeEdit;
    private EditText earScoreEdit;
    private EditText earTimeEdit;
    private EditText ywZxlxScoreEdit;
    private EditText ywZxlxTimeEdit;
    private EditText sxZxlxScoreEdit;
    private EditText sxZxlxTimeEdit;
    private EditText yyZxlxScoreEdit;
    private EditText yyZxlxTimeEdit;
    private EditText smartScoreEdit;
    private EditText smartTimeEdit;
    private EditText increaseTimeEdit;
    private EditText chantTimeEdit;
    private EditText chantNumEdit;
    private EditText chantuseTimeEdit;
    private Switch autoSwitch;
    private Switch manualSwitch;
    private Switch smartSwitch;
    private Switch increaseSwitch;
    private RadioGroup radioGroup;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;

    private LinearLayout synScoreLayout;
    private LinearLayout synTimeLayout;
    private LinearLayout whiteScoreLayout;
    private LinearLayout whiteTimeLayout;
    private LinearLayout spokenScoreLayout;
    private LinearLayout spokenTimeLayout;
    private LinearLayout wordScoreLayout;
    private LinearLayout wordTimeLayout;
    private LinearLayout earScoreLayout;
    private LinearLayout earTimeLayout;
    private LinearLayout ywZxlxScoreLayout;
    private LinearLayout ywZxlxTimeLayout;
    private LinearLayout sxZxlxScoreLayout;
    private LinearLayout sxZxlxTimeLayout;
    private LinearLayout yyZxlxScoreLayout;
    private LinearLayout yyZxlxTimeLayout;
    private LinearLayout smartScoreLayout;
    private LinearLayout smartTimeLayout;
    private LinearLayout chantuseLayout;

    private BabyApplication babyApplication = BabyApplication.getInstance();
    private int defaultFlag = 1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.reward_setting_activity);

        setTitle("学习奖励设置");

        initView();

        getData();

        UmengEventUtil.onEvent(babyApplication,"reward_set_page_id");
    }

    private void initView() {
        clearButton = findViewById(R.id.reward_setting_clear_btn);
        clearButton.setOnClickListener(this);
        submitButton = findViewById(R.id.reward_setting_submit_btn);
        submitButton.setOnClickListener(this);
        synScoreEdit = findViewById(R.id.reward_setting_syn_score_edit);
        synScoreEdit.setOnClickListener(this);
        synTimeEdit = findViewById(R.id.reward_setting_syn_time_edit);
        synTimeEdit.setOnClickListener(this);
        whiteScoreEdit = findViewById(R.id.reward_setting_white_score_edit);
        whiteScoreEdit.setOnClickListener(this);
        whiteTimeEdit = findViewById(R.id.reward_setting_white_time_edit);
        whiteTimeEdit.setOnClickListener(this);
        spokenScoreEdit = findViewById(R.id.reward_setting_spoken_score_edit);
        spokenScoreEdit.setOnClickListener(this);
        spokenTimeEdit = findViewById(R.id.reward_setting_spoken_time_edit);
        spokenTimeEdit.setOnClickListener(this);
        wordScoreEdit = findViewById(R.id.reward_setting_word_score_edit);
        wordScoreEdit.setOnClickListener(this);
        wordTimeEdit = findViewById(R.id.reward_setting_word_time_edit);
        wordTimeEdit.setOnClickListener(this);
        earScoreEdit = findViewById(R.id.reward_setting_ear_score_edit);
        earScoreEdit.setOnClickListener(this);
        earTimeEdit = findViewById(R.id.reward_setting_ear_time_edit);
        earTimeEdit.setOnClickListener(this);
        smartScoreEdit = findViewById(R.id.reward_setting_smart_score_edit);
        smartScoreEdit.setOnClickListener(this);
        smartTimeEdit = findViewById(R.id.reward_setting_smart_time_edit);
        smartTimeEdit.setOnClickListener(this);
        increaseTimeEdit = findViewById(R.id.reward_setting_increase_edit);
        increaseTimeEdit.setOnClickListener(this);
        ywZxlxScoreEdit = findViewById(R.id.reward_setting_yw_zxlx_score_edit);
        ywZxlxScoreEdit.setOnClickListener(this);
        ywZxlxTimeEdit = findViewById(R.id.reward_setting_yw_zxlx_time_edit);
        ywZxlxTimeEdit.setOnClickListener(this);
        sxZxlxScoreEdit = findViewById(R.id.reward_setting_sx_zxlx_score_edit);
        sxZxlxScoreEdit.setOnClickListener(this);
        sxZxlxTimeEdit = findViewById(R.id.reward_setting_sx_zxlx_time_edit);
        sxZxlxTimeEdit.setOnClickListener(this);
        yyZxlxScoreEdit = findViewById(R.id.reward_setting_yy_zxlx_score_edit);
        yyZxlxScoreEdit.setOnClickListener(this);
        yyZxlxTimeEdit = findViewById(R.id.reward_setting_yy_zxlx_time_edit);
        yyZxlxTimeEdit.setOnClickListener(this);
        chantTimeEdit = findViewById(R.id.reward_setting_chant_time_edit);
        chantTimeEdit.setOnClickListener(this);
        chantNumEdit = findViewById(R.id.reward_setting_chant_num_edit);
        chantNumEdit.setOnClickListener(this);
        chantuseTimeEdit = findViewById(R.id.reward_setting_chant_use_time_edit);
        chantuseTimeEdit.setOnClickListener(this);
        autoSwitch = findViewById(R.id.reward_setting_auto_switch);
        manualSwitch = findViewById(R.id.reward_setting_manual_switch);
        smartSwitch = findViewById(R.id.reward_setting_smart_switch);
        increaseSwitch = findViewById(R.id.reward_setting_increase_switch);
        radioGroup = findViewById(R.id.reward_setting_exercise_num_rg);
        radioGroup.setOnCheckedChangeListener(this);
        radioButton1 = findViewById(R.id.reward_setting_exercise_num_rb1);
        radioButton2 = findViewById(R.id.reward_setting_exercise_num_rb2);
        radioButton3 = findViewById(R.id.reward_setting_exercise_num_rb3);

        synScoreLayout = findViewById(R.id.reward_setting_syn_score_layout);
        synScoreLayout.setOnClickListener(this);
        synTimeLayout = findViewById(R.id.reward_setting_syn_time_layout);
        synTimeLayout.setOnClickListener(this);
        whiteScoreLayout = findViewById(R.id.reward_setting_white_score_layout);
        whiteScoreLayout.setOnClickListener(this);
        whiteTimeLayout = findViewById(R.id.reward_setting_white_time_layout);
        whiteTimeLayout.setOnClickListener(this);
        spokenScoreLayout = findViewById(R.id.reward_setting_spoken_score_layout);
        spokenScoreLayout.setOnClickListener(this);
        spokenTimeLayout = findViewById(R.id.reward_setting_spoken_time_layout);
        spokenTimeLayout.setOnClickListener(this);
        wordScoreLayout = findViewById(R.id.reward_setting_word_score_layout);
        wordScoreLayout.setOnClickListener(this);
        wordTimeLayout = findViewById(R.id.reward_setting_word_time_layout);
        wordTimeLayout.setOnClickListener(this);
        earScoreLayout = findViewById(R.id.reward_setting_ear_score_layout);
        earScoreLayout.setOnClickListener(this);
        earTimeLayout = findViewById(R.id.reward_setting_ear_time_layout);
        earTimeLayout.setOnClickListener(this);
        ywZxlxScoreLayout = findViewById(R.id.reward_setting_yw_zxlx_score_layout);
        ywZxlxScoreLayout.setOnClickListener(this);
        ywZxlxTimeLayout = findViewById(R.id.reward_setting_yw_zxlx_time_layout);
        ywZxlxTimeLayout.setOnClickListener(this);
        sxZxlxScoreLayout = findViewById(R.id.reward_setting_sx_zxlx_score_layout);
        sxZxlxScoreLayout.setOnClickListener(this);
        sxZxlxTimeLayout = findViewById(R.id.reward_setting_sx_zxlx_time_layout);
        sxZxlxTimeLayout.setOnClickListener(this);
        yyZxlxScoreLayout = findViewById(R.id.reward_setting_yy_zxlx_score_layout);
        yyZxlxScoreLayout.setOnClickListener(this);
        yyZxlxTimeLayout = findViewById(R.id.reward_setting_yy_zxlx_time_layout);
        yyZxlxTimeLayout.setOnClickListener(this);
        smartScoreLayout = findViewById(R.id.reward_setting_smart_score_layout);
        smartScoreLayout.setOnClickListener(this);
        smartTimeLayout = findViewById(R.id.reward_setting_smart_time_layout);
        smartTimeLayout.setOnClickListener(this);
        chantuseLayout = findViewById(R.id.reward_setting_chant_use_time_layout);
        chantuseLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reward_setting_syn_score_layout:
            case R.id.reward_setting_syn_score_edit:
                showNumberPickWindow(1,synScoreEdit,"同步练习获得分数");
                break;
            case R.id.reward_setting_syn_time_layout:
            case R.id.reward_setting_syn_time_edit:
                showNumberPickWindow(2,synTimeEdit,"同步练习奖励时间");
                break;
            case R.id.reward_setting_white_score_layout:
            case R.id.reward_setting_white_score_edit:
                showNumberPickWindow(1,whiteScoreEdit,"生字练习获得分数");
                break;
            case R.id.reward_setting_white_time_layout:
            case R.id.reward_setting_white_time_edit:
                showNumberPickWindow(2,whiteTimeEdit,"生字练习奖励时间");
                break;
            case R.id.reward_setting_spoken_score_layout:
            case R.id.reward_setting_spoken_score_edit:
                showNumberPickWindow(1,spokenScoreEdit,"口语练习获得分数");
                break;
            case R.id.reward_setting_spoken_time_layout:
            case R.id.reward_setting_spoken_time_edit:
                showNumberPickWindow(2,synScoreEdit,"口语练习奖励时间");
                break;
            case R.id.reward_setting_word_score_layout:
            case R.id.reward_setting_word_score_edit:
                showNumberPickWindow(1,wordScoreEdit,"单词练习获得分数");
                break;
            case R.id.reward_setting_word_time_layout:
            case R.id.reward_setting_word_time_edit:
                showNumberPickWindow(2,wordTimeEdit,"单词练习奖励时间");
                break;
            case R.id.reward_setting_ear_score_layout:
            case R.id.reward_setting_ear_score_edit:
                showNumberPickWindow(1,earScoreEdit,"听力练习获得分数");
                break;
            case R.id.reward_setting_ear_time_layout:
            case R.id.reward_setting_ear_time_edit:
                showNumberPickWindow(2,earTimeEdit,"听力练习奖励时间");
                break;
            case R.id.reward_setting_yw_zxlx_score_layout:
            case R.id.reward_setting_yw_zxlx_score_edit:
                showNumberPickWindow(1,ywZxlxScoreEdit,"语文分科练习获得分数");
                break;
            case R.id.reward_setting_yw_zxlx_time_layout:
            case R.id.reward_setting_yw_zxlx_time_edit:
                showNumberPickWindow(2,ywZxlxTimeEdit,"语文分科练习奖励时间");
                break;
            case R.id.reward_setting_sx_zxlx_score_layout:
            case R.id.reward_setting_sx_zxlx_score_edit:
                showNumberPickWindow(1,sxZxlxScoreEdit,"数学分科练习获得分数");
                break;
            case R.id.reward_setting_sx_zxlx_time_layout:
            case R.id.reward_setting_sx_zxlx_time_edit:
                showNumberPickWindow(2,sxZxlxTimeEdit,"数学分科练习奖励时间");
                break;
            case R.id.reward_setting_yy_zxlx_score_layout:
            case R.id.reward_setting_yy_zxlx_score_edit:
                showNumberPickWindow(1,yyZxlxScoreEdit,"英语分科练习获得分数");
                break;
            case R.id.reward_setting_yy_zxlx_time_layout:
            case R.id.reward_setting_yy_zxlx_time_edit:
                showNumberPickWindow(2,yyZxlxTimeEdit,"英语分科练习奖励时间");
                break;
            case R.id.reward_setting_chant_use_time_layout:
            case R.id.reward_setting_chant_use_time_edit:
                showNumberPickWindow(2,earTimeEdit,"口算练习奖励时间");
                break;
            case R.id.reward_setting_chant_time_edit:
                showNumberPickWindow(3,chantTimeEdit,"口算练习分钟选择");
                break;
            case R.id.reward_setting_chant_num_edit:
                showNumberPickWindow(4,chantNumEdit,"口算练习正确题目数");
                break;
            case R.id.reward_setting_increase_edit:
                showNumberPickWindow(3,increaseTimeEdit,"递增奖励时间选择");
                break;
            case R.id.reward_setting_smart_score_layout:
            case R.id.reward_setting_smart_score_edit:
                showNumberPickWindow(1,smartScoreEdit,"智能练习获得分数");
                break;
            case R.id.reward_setting_smart_time_layout:
            case R.id.reward_setting_smart_time_edit:
                showNumberPickWindow(2,smartTimeEdit,"智能练习奖励时间");
                break;
            case R.id.reward_setting_clear_btn:
                clearEdit();
                break;
            case R.id.reward_setting_submit_btn:
                if(valide()){
                    submit();
                }
                break;
            default:
                break;
        }
    }

    private void clearEdit() {
        autoSwitch.setChecked(true);
        manualSwitch.setChecked(true);
        smartSwitch.setChecked(false);
        increaseSwitch.setChecked(false);
        radioButton2.setChecked(true);

        synScoreEdit.setText("");
        synTimeEdit.setText("");
        whiteScoreEdit.setText("");
        whiteTimeEdit.setText("");
        spokenScoreEdit.setText("");
        spokenTimeEdit.setText("");
        wordScoreEdit.setText("");
        wordTimeEdit.setText("");
        earScoreEdit.setText("");
        earTimeEdit.setText("");
        ywZxlxScoreEdit.setText("");
        ywZxlxTimeEdit.setText("");
        sxZxlxScoreEdit.setText("");
        sxZxlxTimeEdit.setText("");
        yyZxlxScoreEdit.setText("");
        yyZxlxTimeEdit.setText("");
        smartScoreEdit.setText("");
        smartTimeEdit.setText("");
        chantTimeEdit.setText("");
        chantNumEdit.setText("");
        chantuseTimeEdit.setText("");
        increaseTimeEdit.setText("");
    }

    private void getData() {
        int childId = babyApplication.getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.GET_NEWREWARD_LIST);
        urlParse.putValue("child_id", childId);

        showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<RewardBean>() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(RewardBean rewardBean) {
                        autoSwitch.setChecked(rewardBean.auto_progress==1?true:false);
                        manualSwitch.setChecked(rewardBean.manual_progress==1?true:false);
                        smartSwitch.setChecked(rewardBean.priority_completion==1?true:false);
                        increaseSwitch.setChecked(rewardBean.reward_increments==1?true:false);
                        if(rewardBean.num.equals("10")){
                            radioButton1.setChecked(true);
                        }else if(rewardBean.num.equals("20")){
                            radioButton2.setChecked(true);
                        }else if(rewardBean.num.equals("30")){
                            radioButton3.setChecked(true);
                        }

                        increaseTimeEdit.setText(rewardBean.increments_num);

                        ScoreTimeBean scoreTimeBean = rewardBean.getRewardMap().get(Constants.TYPE_SYNC_EXERCISE + "");
                        if(scoreTimeBean != null){
                            synScoreEdit.setText(scoreTimeBean.score + "");
                            synTimeEdit.setText(scoreTimeBean.time + "");
                        }

                        scoreTimeBean = rewardBean.getRewardMap().get(Constants.TYPE_STRANGE_WORDS_EXERCISE + "");
                        if(scoreTimeBean != null){
                            whiteScoreEdit.setText(scoreTimeBean.score + "");
                            whiteTimeEdit.setText(scoreTimeBean.time + "");
                        }

                        scoreTimeBean = rewardBean.getRewardMap().get(Constants.TYPE_SPEAKING_EXERCISE + "");
                        if(scoreTimeBean != null){
                            spokenScoreEdit.setText(scoreTimeBean.score + "");
                            spokenTimeEdit.setText(scoreTimeBean.time + "");
                        }

                        scoreTimeBean = rewardBean.getRewardMap().get(Constants.TYPE_ENGLISH_WORDS_EXERCISE + "");
                        if(scoreTimeBean != null){
                            wordScoreEdit.setText(scoreTimeBean.score + "");
                            wordTimeEdit.setText(scoreTimeBean.time + "");
                        }

                        scoreTimeBean = rewardBean.getRewardMap().get(Constants.TYPE_HEARING_EXERCISE + "");
                        if(scoreTimeBean != null){
                            earScoreEdit.setText(scoreTimeBean.score + "");
                            earTimeEdit.setText(scoreTimeBean.time + "");
                        }

                        scoreTimeBean = rewardBean.getRewardMap().get(Constants.TYPE_SMART_EXERCISE + "");
                        if(scoreTimeBean != null){
                            smartScoreEdit.setText(scoreTimeBean.score + "");
                            smartTimeEdit.setText(scoreTimeBean.time + "");
                        }

                        scoreTimeBean = rewardBean.getRewardMap().get(Constants.TYPE_CHINESE_SECT_EXERCISE + "");
                        if(scoreTimeBean != null){
                            ywZxlxScoreEdit.setText(scoreTimeBean.score + "");
                            ywZxlxTimeEdit.setText(scoreTimeBean.time + "");
                        }

                        scoreTimeBean = rewardBean.getRewardMap().get(Constants.TYPE_MACH_SECT_EXERCISE + "");
                        if(scoreTimeBean != null){
                            sxZxlxScoreEdit.setText(scoreTimeBean.score + "");
                            sxZxlxTimeEdit.setText(scoreTimeBean.time + "");
                        }

                        scoreTimeBean = rewardBean.getRewardMap().get(Constants.TYPE_ENGLISH_SECT_EXERCISE + "");
                        if(scoreTimeBean != null){
                            yyZxlxScoreEdit.setText(scoreTimeBean.score + "");
                            yyZxlxTimeEdit.setText(scoreTimeBean.time + "");
                        }

                        scoreTimeBean = rewardBean.getRewardMap().get(Constants.TYPE_ORAL_CALCULATE_EXERCISE + "");
                        if(scoreTimeBean != null){
                            chantTimeEdit.setText(scoreTimeBean.total + "");
                            chantNumEdit.setText(scoreTimeBean.num + "");
                            chantuseTimeEdit.setText(scoreTimeBean.time + "");
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.reward_setting_exercise_num_rb1:
                break;
            case R.id.reward_setting_exercise_num_rb2:
                break;
            case R.id.reward_setting_exercise_num_rb3:
                break;
            default:
                break;
        }
    }

    private boolean valide() {
        String score = synScoreEdit.getText().toString();
        if(!TextUtils.isEmpty(score) && (Integer.parseInt(score) < 70 || Integer.parseInt(score) > 100)){
            showToast("请输入70到100范围的同步练习成绩");
            return false;
        }

        String min = synTimeEdit.getText().toString();
        if(!TextUtils.isEmpty(min) && (Integer.parseInt(min) > 180)){
            showToast("同步练习输入的时间不能大于180分钟");
            return false;
        }

        score = whiteScoreEdit.getText().toString();
        if(!TextUtils.isEmpty(score) && (Integer.parseInt(score) < 70 || Integer.parseInt(score) > 100)){
            showToast("请输入70到100范围的生词练习成绩");
            return false;
        }

        min = whiteTimeEdit.getText().toString();
        if(!TextUtils.isEmpty(min) && (Integer.parseInt(min) > 180)){
            showToast("生词练习输入的时间不能大于180分钟");
            return false;
        }

        score = spokenScoreEdit.getText().toString();
        if(!TextUtils.isEmpty(score) && (Integer.parseInt(score) < 70 || Integer.parseInt(score) > 100)){
            showToast("请输入70到100范围的口语练习成绩");
            return false;
        }

        min = spokenTimeEdit.getText().toString();
        if(!TextUtils.isEmpty(min) && (Integer.parseInt(min) > 180)){
            showToast("口语练习输入的时间不能大于180分钟");
            return false;
        }

        score = wordScoreEdit.getText().toString();
        if(!TextUtils.isEmpty(score) && (Integer.parseInt(score) < 70 || Integer.parseInt(score) > 100)){
            showToast("请输入70到100范围的单词练习成绩");
            return false;
        }

        min = wordTimeEdit.getText().toString();
        if(!TextUtils.isEmpty(min) && (Integer.parseInt(min) > 180)){
            showToast("单词练习输入的时间不能大于180分钟");
            return false;
        }

        score = earScoreEdit.getText().toString();
        if(!TextUtils.isEmpty(score) && (Integer.parseInt(score) < 70 || Integer.parseInt(score) > 100)){
            showToast("请输入70到100范围的听力练习成绩");
            return false;
        }

        min = earTimeEdit.getText().toString();
        if(!TextUtils.isEmpty(min) && (Integer.parseInt(min) > 180)){
            showToast("听力练习输入的时间不能大于180分钟");
            return false;
        }

        score = smartScoreEdit.getText().toString();
        if(!TextUtils.isEmpty(score) && (Integer.parseInt(score) < 70 || Integer.parseInt(score) > 100)){
            showToast("请输入70到100范围的边学边练成绩");
            return false;
        }

        min = smartTimeEdit.getText().toString();
        if(!TextUtils.isEmpty(min) && (Integer.parseInt(min) > 180)){
            showToast("边学边练输入的时间不能大于180分钟");
            return false;
        }

        String time = chantTimeEdit.getText().toString();
        if(!TextUtils.isEmpty(time) && (Integer.parseInt(time) < 1 || Integer.parseInt(time) > 10)){
            showToast("口算练习输入的时间不能小于1分钟和大于10分钟");
            return false;
        }

        String num = chantNumEdit.getText().toString();
        if(!TextUtils.isEmpty(num) && (Integer.parseInt(num) < 10 || Integer.parseInt(num) > 100)){
            showToast("口算练习输入的题数不能小于10和大于100");
            return false;
        }

        min = chantuseTimeEdit.getText().toString();
        if(!TextUtils.isEmpty(min) && (Integer.parseInt(min) > 180)){
            showToast("口算练习输入的时间不能大于180分钟");
            return false;
        }

        return true;
    }

    private void submit() {
        showWaitDialog();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.POST_NEWREWARD_SET);
        urlParse.putValue("child_id", babyApplication.getCurrentChildId())
                .putValue("setting",getSettingJson())
                .putValue("default",defaultFlag);

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        onBackAction();
                        showToast("设置成功");
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    private String getSettingJson() {
        JSONObject jsonObject = new JSONObject();

        try {
            if(autoSwitch.isChecked()){
                jsonObject.put("auto_progress",1);
            }else {
                jsonObject.put("auto_progress",0);
                defaultFlag = 0;
            }

            if(manualSwitch.isChecked()){
                jsonObject.put("manual_progress",1);
            }else {
                jsonObject.put("manual_progress",0);
                defaultFlag = 0;
            }

            if(smartSwitch.isChecked()){
                jsonObject.put("priority_completion",1);
                defaultFlag = 0;
            }else {
                jsonObject.put("priority_completion",0);
            }

            String incrementsNum = increaseTimeEdit.getText().toString();
            if(increaseSwitch.isChecked()){
                jsonObject.put("reward_increments",1);
                defaultFlag = 0;

                if(TextUtils.isEmpty(incrementsNum)){
                    incrementsNum = increaseTimeEdit.getHint().toString();
                }else {
                    defaultFlag = 0;
                }
                jsonObject.put("increments_num",incrementsNum);
            }else {
                jsonObject.put("reward_increments",0);
                jsonObject.put("increments_num",incrementsNum);
            }

            if(radioButton1.isChecked()){
                jsonObject.put("num",10);
                defaultFlag = 0;
            }else if(radioButton2.isChecked()){
                jsonObject.put("num",20);
            }else if(radioButton3.isChecked()){
                jsonObject.put("num",30);
                defaultFlag = 0;
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        JSONObject rewardJson = new JSONObject();
        try {
            JSONObject json = new JSONObject();
            String score = synScoreEdit.getText().toString();
            if(TextUtils.isEmpty(score)){
                score = synScoreEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("score",score);

            String time = synTimeEdit.getText().toString();
            if(TextUtils.isEmpty(time)){
                time = synTimeEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("time",time);
            rewardJson.put(Constants.TYPE_SYNC_EXERCISE + "",json);

            json = new JSONObject();
            score = whiteScoreEdit.getText().toString();
            if(TextUtils.isEmpty(score)){
                score = whiteScoreEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("score",score);

            time = whiteTimeEdit.getText().toString();
            if(TextUtils.isEmpty(time)){
                time = whiteTimeEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("time",time);
            rewardJson.put(Constants.TYPE_STRANGE_WORDS_EXERCISE + "",json);

            json = new JSONObject();
            score = spokenScoreEdit.getText().toString();
            if(TextUtils.isEmpty(score)){
                score = spokenScoreEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("score",score);

            time = spokenTimeEdit.getText().toString();
            if(TextUtils.isEmpty(time)){
                time = spokenTimeEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("time",time);
            rewardJson.put(Constants.TYPE_SPEAKING_EXERCISE + "",json);

            json = new JSONObject();
            score = wordScoreEdit.getText().toString();
            if(TextUtils.isEmpty(score)){
                score = wordScoreEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("score",score);

            time = wordTimeEdit.getText().toString();
            if(TextUtils.isEmpty(time)){
                time = wordTimeEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("time",time);
            rewardJson.put(Constants.TYPE_ENGLISH_WORDS_EXERCISE + "",json);

            json = new JSONObject();
            score = earScoreEdit.getText().toString();
            if(TextUtils.isEmpty(score)){
                score = earScoreEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("score",score);

            time = earTimeEdit.getText().toString();
            if(TextUtils.isEmpty(time)){
                time = earTimeEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("time",time);
            rewardJson.put(Constants.TYPE_HEARING_EXERCISE + "",json);

            json = new JSONObject();
            score = smartScoreEdit.getText().toString();
            if(TextUtils.isEmpty(score)){
                score = smartScoreEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("score",score);

            time = smartTimeEdit.getText().toString();
            if(TextUtils.isEmpty(time)){
                time = smartTimeEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("time",time);
            rewardJson.put(Constants.TYPE_SMART_EXERCISE + "",json);

            json = new JSONObject();
            score = ywZxlxScoreEdit.getText().toString();
            if(TextUtils.isEmpty(score)){
                score = ywZxlxScoreEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("score",score);

            time = ywZxlxTimeEdit.getText().toString();
            if(TextUtils.isEmpty(time)){
                time = ywZxlxTimeEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("time",time);
            rewardJson.put(Constants.TYPE_CHINESE_SECT_EXERCISE + "",json);

            json = new JSONObject();
            score = sxZxlxScoreEdit.getText().toString();
            if(TextUtils.isEmpty(score)){
                score = sxZxlxScoreEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("score",score);

            time = sxZxlxTimeEdit.getText().toString();
            if(TextUtils.isEmpty(time)){
                time = sxZxlxTimeEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("time",time);
            rewardJson.put(Constants.TYPE_MACH_SECT_EXERCISE + "",json);

            json = new JSONObject();
            score = yyZxlxScoreEdit.getText().toString();
            if(TextUtils.isEmpty(score)){
                score = yyZxlxScoreEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("score",score);

            time = yyZxlxTimeEdit.getText().toString();
            if(TextUtils.isEmpty(time)){
                time = yyZxlxTimeEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("time",time);
            rewardJson.put(Constants.TYPE_ENGLISH_SECT_EXERCISE + "",json);

            json = new JSONObject();
            String total = chantTimeEdit.getText().toString();
            if(TextUtils.isEmpty(total)){
                total = chantTimeEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("total",total);

            String num = chantNumEdit.getText().toString();
            if(TextUtils.isEmpty(num)){
                num = chantNumEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("num",num);

            time = chantuseTimeEdit.getText().toString();
            if(TextUtils.isEmpty(time)){
                time = chantuseTimeEdit.getHint().toString();
            }else {
                defaultFlag = 0;
            }
            json.put("time",time);
            rewardJson.put(Constants.TYPE_ORAL_CALCULATE_EXERCISE + "",json);

            jsonObject.put("reward_time",rewardJson);
        }catch (Exception e){
            e.printStackTrace();
        }

        if(defaultFlag == 1){
            return "";
        }else {
            return jsonObject.toString();
        }
    }

    private void showNumberPickWindow(int type,final EditText editText,String title) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.number_pick_pop_bottom, null);

        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setFocusable(true);//弹出窗体可点击
        popupWindow.setAnimationStyle(R.style.numberPopupAnimation);

        final NumberPicker numberPicker = contentView.findViewById(R.id.bottom_window_number_pick);
        numberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

        DensityUtil.setPickerDividerHeight(numberPicker,DensityUtil.dip2px(this,0.5));
        DensityUtil.setPickerDividerColor(numberPicker,Color.parseColor("#DBDBDB"));

        TextView titleText = contentView.findViewById(R.id.bottom_window_title_tv);
        titleText.setText(title);

        TextView colseText = contentView.findViewById(R.id.bottom_window_close_tv);
        colseText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        TextView okText = contentView.findViewById(R.id.bottom_window_ok_tv);
        okText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();

                editText.setText(numberPicker.getDisplayedValues()[numberPicker.getValue()]);
            }
        });

        List<String> displayList = new ArrayList<String>();
        if(type == 1){
            for (int i = 70;i <= 100;i++){
                if(i % 5 == 0){
                    displayList.add(i + "");
                }
            }
        }else if (type == 2){
            for (int i = 0;i <= 180;i++){
                if(i % 5 == 0){
                    displayList.add(i + "");
                }
            }
        } else if (type == 3){
            for (int i = 1;i <= 10;i++){
                displayList.add(i + "");
            }
        }else if (type == 4){
            for (int i = 10;i <= 100;i++){
                if(i % 5 == 0){
                    displayList.add(i + "");
                }
            }
        }

        int oldMax = numberPicker.getMaxValue();
        int newMax = displayList.size() - 1;
        if(newMax > oldMax) {
            numberPicker.setDisplayedValues((String[])displayList.toArray(new String[displayList.size()]));
            numberPicker.setMaxValue(newMax);
        } else {
            numberPicker.setMaxValue(newMax);
            numberPicker.setDisplayedValues((String[])displayList.toArray(new String[displayList.size()]));
        }

        //从底部显示
        popupWindow.showAtLocation(clearButton, Gravity.BOTTOM, 0, 0);
    }
}
